package com.pig4cloud.pig.module.controller.devices;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.devices.DevicesEggCell;
import com.pig4cloud.pig.module.service.devices.IDevicesEggCellService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 鸡蛋柜的格子列表Controller
 *
 * @author ruoyi
 * @date 2022-01-06
 */
@Validated
@Api(value = "鸡蛋柜的格子列表控制器", tags = {"鸡蛋柜的格子列表管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/devices/cell")
public class DevicesEggCellController {

    private final IDevicesEggCellService devicesEggCellService;

    /**
     * 查询鸡蛋柜的格子列表列表
     */
    @ApiOperation("查询鸡蛋柜的格子列表列表")
    @PreAuthorize("@pms.hasPermission('module:deviceCell:list')")
    @GetMapping("/list")
    public TableDataInfo<DevicesEggCell> list(@Validated DevicesEggCell devicesEggCell) {
        QueryWrapper<DevicesEggCell> qw = new QueryWrapper<>();
        if(devicesEggCell.getDeviceId()!=null){
        	qw.eq("device_id",devicesEggCell.getDeviceId());
		}
        TableDataInfo<DevicesEggCell> td = PageUtils.buildDataInfo(devicesEggCellService.page(PageUtils.buildPage(),qw));
        List<DevicesEggCell> list = td.getData();
        return td;
    }

    /**
     * 导出鸡蛋柜的格子列表列表
     */
    @ApiOperation("导出鸡蛋柜的格子列表列表")
    @PreAuthorize("@pms.hasPermission('module:deviceCell:export')")
    @SysLog("鸡蛋柜的格子列表")
    @GetMapping("/export")
    public List export(@Validated DevicesEggCell devicesEggCell) {
        List<DevicesEggCell> list = devicesEggCellService.list();
        // ExcelUtil<DevicesEggCell> util = new // ExcelUtil<DevicesEggCell>(DevicesEggCell.class);
        return  list;// util.exportExcel(list, "鸡蛋柜的格子列表");
    }

    /**
     * 获取鸡蛋柜的格子列表详细信息
     */
    @ApiOperation("获取鸡蛋柜的格子列表详细信息")
    @PreAuthorize("@pms.hasPermission('module:deviceCell:query')")
    @GetMapping("/{id}")
    public R<DevicesEggCell> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        DevicesEggCell devicesEggCell = devicesEggCellService.getById(id);
        return R.ok(devicesEggCell);
    }

    /**
     * 新增鸡蛋柜的格子列表
     */
    @ApiOperation("新增鸡蛋柜的格子列表")
    @PreAuthorize("@pms.hasPermission('module:deviceCell:add')")
    @SysLog("鸡蛋柜的格子列表")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody DevicesEggCell devicesEggCell) {
        return R.okByRow(devicesEggCellService.save(devicesEggCell) ? 1 : 0);
    }

    /**
     * 修改鸡蛋柜的格子列表
     */
    @ApiOperation("修改鸡蛋柜的格子列表")
    @PreAuthorize("@pms.hasPermission('module:deviceCell:edit')")
    @SysLog("鸡蛋柜的格子列表")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody DevicesEggCell devicesEggCell) {
        return R.okByRow(devicesEggCellService.updateById(devicesEggCell) ? 1 : 0);
    }

    /**
     * 删除鸡蛋柜的格子列表
     */
    @ApiOperation("删除鸡蛋柜的格子列表")
    @PreAuthorize("@pms.hasPermission('module:deviceCell:remove')")
    @SysLog("鸡蛋柜的格子列表")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(devicesEggCellService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
